import { LOOP_MODE } from "@/global/const";

export const useMusicStore = defineStore(
  'music',
  () => {
    // 本地音乐列表
    const localMusicList = ref<AudiopalyRecord[]>(localCache.getItem('localMusicList') || [])

    // 当前播放列表
    const currentMusicList = ref<AudiopalyRecord[]>(localCache.getItem('currentMusicList') || [])

    // 当前播放音乐
    const currentMusic = ref<AudiopalyRecord>(localCache.getItem('currentMusic') || {})

    // 循环模式
    const loopType = ref<LOOP_MODE>(LOOP_MODE.LOOP);

    /** 设置音乐列表 */
    function setLocalMusicList(list: AudiopalyRecord[]) {
      localMusicList.value = list;
      localCache.setItem('localMusicList', list);
    }

    /** 设置当前播放列表 */
    function setCurrentMusicList(list: AudiopalyRecord[]) {
      currentMusicList.value = list;
      localCache.setItem('currentMusicList', list);
    }

    /** 设置当前播放音乐 */
    function setCurrentMusic(music: AudiopalyRecord) {
      currentMusic.value = music;
      localCache.setItem('currentMusic', music);
    }

    /** 设置循环模式 */
    function setLoopType(type: LOOP_MODE) {
      loopType.value = type;
    }

    return {
      localMusicList,
      setLocalMusicList,
      currentMusicList,
      setCurrentMusicList,
      currentMusic,
      setCurrentMusic,
      loopType,
      setLoopType,
    }
  }
)
